package com.shop.shopmall.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 
 * @TableName user
 */
@TableName(value ="user")
@Data
public class User implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 账号
     */
    @TableField("user_name")
    private String username;

    /**
     * 密码
     */
    @TableField("pass_word")
    private String password;

    /**
     * 手机号
     */
    private String phone;

    /**
     * 状态 1-有效 0-无效
     */
    private String status;

    /**
     * 更新时间
     */
    private Date updateDete;

    /**
     * 性别
     */
    private String sex;

    @TableField(exist = false)
    private String roleName;

    @TableField(exist = false)
    private Integer[] roles;

    @TableField(exist = false)
    private List<Role> roleList = new ArrayList<>();

    @TableField(exist = false)
    private List<Menu> menus = new ArrayList<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 获取权限，把权限字符串封装成SimpleGrantedAuthority对象
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Menu menu : menus) {
            authorities.add(new SimpleGrantedAuthority(menu.getPath()));
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        if (status.equals("1")) {
            return true;
        }
        return false;
    }

}